/*
 * Copyright 2008 Andrej Herich
 * 
 * http://azetclient.yw.sk/
 * http://azetclient.googlecode.com/
 * 
 * This file is part of AzetClient.
 * 
 * AzetClient is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AzetClient is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AzetClient.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.yw.azetclient.model;

import java.util.Date;

/**
 *
 * @author error216
 */
public abstract class AbstractMessage implements Message {
    
    protected long id = 0L;
    protected Buddy sender;
    protected Buddy receiver;
    protected Date sendTime;
    protected String content;
    protected MessageThread thread;
    
    public AbstractMessage(MessageBean messageBean, MessageThread thread) {
        if (messageBean == null)
            throw new IllegalArgumentException("Null pointer in messageBean");
        
        id = messageBean.getId();
        sender = messageBean.getSender();
        receiver = messageBean.getReceiver();
        
        Date beanSendTime = messageBean.getSendTime();
        if (beanSendTime == null) {
            sendTime = null;
        } else {
            sendTime = (Date) beanSendTime.clone();
        }
        
        content = messageBean.getContent();
        this.thread = thread;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Message) {
            Message message = (Message) obj;
            if (id != 0L) {
                return id == message.getId();
            } else if (sender != null && receiver != null
                    && sendTime != null && content != null) {
                return sender.equals(message.getSender())
                        && receiver.equals(message.getReceiver())
                        && sendTime.equals(message.getSendTime())
                        && content.equals(message.getContent());
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        if (id != 0L) {
            return (new Long(id % Integer.MAX_VALUE)).intValue();
        } else if (sender != null && receiver != null
                    && sendTime != null && content != null) {
            int hash = 7;
            hash = 11 * hash + (this.sender != null ? this.sender.hashCode() : 0);
            hash = 11 * hash + (this.receiver != null ? this.receiver.hashCode() : 0);
            hash = 11 * hash + (this.sendTime != null ? this.sendTime.hashCode() : 0);
            hash = 11 * hash + (this.content != null ? this.content.hashCode() : 0);
            return hash;
        } else {
            return 0;
        }
    }
    
    public long getId() {
        return id;
    }

    public Buddy getSender() {
        return sender;
    }

    public Buddy getReceiver() {
        return receiver;
    }

    public Date getSendTime() {
        if (sendTime == null) {
            return null;
        } else {
            return (Date) sendTime.clone();
        }
    }

    public String getContent() {
        return content;
    }

    public MessageThread getThread() {
        return thread;
    }

}
